local dengnan = fk.CreateSkill {
  name = "dengnan",
  tags={Skill.Limited}
}

dengnan:addEffect("viewas", {
  anim_type = "control",
  prompt = "#dengnan-invoke",
  interaction = function(self,player)
    local names = {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if card:isCommonTrick() and not card.is_damage_card and not card.is_derived and
      player:canUse(card) and not player:prohibitUse(card) then
        table.insertIfNeed(names, card.name)
      end
    end
    return U.CardNameBox {choices = names}
  end,
  card_filter = Util.FalseFunc,
  view_as = function(self, cards)
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryGame) == 0
  end,
})

dengnan:addEffect(fk.TurnEnd, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if target == player and player:usedSkillTimes("dengnan", Player.HistoryTurn) > 0 and player:hasSkill("dengnan", true) then
      local mark = player:getMark("dengnan-turn")
      if mark == 0 then mark = {} end
      player.room.logic:getEventsOfScope(GameEvent.ChangeHp, 999, function(e)
        local damage = e.data
        if damage then
          table.removeOne(mark, damage.who.id)
        end
      end, Player.HistoryTurn)
      return #mark == 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player:broadcastSkillInvoke("dengnan")
    player.room:notifySkillInvoked(player, "dengnan", "special")
    player:setSkillUseHistory("dengnan", 0, Player.HistoryGame)
  end,


})

dengnan:addEffect(fk.TargetSpecified, {
  can_refresh = function(self, event, target, player, data)
    return target == player and table.contains(data.card.skillNames, "dengnan")
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "dengnan-turn", table.map(data:getAllTargets(),Util.IdMapper))
    for _, p in ipairs(data:getAllTargets()) do
      room:setPlayerMark(p, "@@dengnan-turn", 1)
    end
  end,
})

return dengnan